MPT.addObject('SocialUtil', function() {

    //好友分组排序
    this.sortFriendGroup = function(array) {
        var returnArray = array;
        var _temp = null, unsort = null;
        $.each(returnArray, function(_i, _r) {
            if (_r && _r.groupName.contains("未分组")) {
                unsort = returnArray.splice(_i, 1)[0];
            }
        });
        for (var i = 0; i < returnArray.length; i++) {
            for (var j = 0; j < returnArray.length - i; j++) {
                if (returnArray[j + 1] != undefined) {
                    if (returnArray[j].positions != undefined && returnArray[j + 1].positions != undefined) {
                        if (returnArray[j].positions > returnArray[j + 1].positions) {
                            _temp = returnArray[j];
                            returnArray[j] = returnArray[j + 1];
                            returnArray[j + 1] = _temp;
                        }
                    }
                }
            }
        }
        returnArray.push(unsort);
        return returnArray;
    }

    //好友关注排序
    this.sortFollowGroup = function(array) {
        var returnArray = array;
        var _temp = null, unsort, all, flw_me, my_flw, flw_each;
        for (var i = 0; i < returnArray.length; i++) {
            var r = returnArray[i];
            if (r) {
                switch (r.groupName) {
                    case "全部": all = returnArray.splice(i, 1)[0]; i--; break;
                    case "未分组": unsort = returnArray.splice(i, 1)[0]; i--; break;
                    case "我关注": my_flw = returnArray.splice(i, 1)[0]; i--; break;
                    case "关注我": flw_me = returnArray.splice(i, 1)[0]; i--; break;
                    case "相互关注": flw_each = returnArray.splice(i, 1)[0]; i--; break;
                }
            }
        };

        returnArray.push(all);
        for (var i = 0; i < returnArray.length; i++) {
            for (var j = 0; j < returnArray.length - i; j++) {
                if (returnArray[j + 1] != undefined) {
                    if (returnArray[j].positions != undefined && returnArray[j + 1].positions != undefined) {
                        if (returnArray[j].positions > returnArray[j + 1].positions) {
                            _temp = returnArray[j];
                            returnArray[j] = returnArray[j + 1];
                            returnArray[j + 1] = _temp;
                        }
                    }
                }
            }
        }
        returnArray.push(unsort);

        return { "result": returnArray, "all": all, "unsort": unsort, "myflw": my_flw, "flwme": flw_me, "flweach": flw_each };
    }

    //创建选择分组
    this.createModifyGroup = function(_ginfo) {

        var group_ids = _ginfo.groupID.toString().split(',');
        var group_names = _ginfo.groupName.split(',');
        var group_list = _ginfo.groupList;

        var modify_type;
        var html = [];
        var has_group = false;

        $.each(group_names, function(_i, _d) {
            if (_d != "" && !_d.contains("全部") && !_d.contains("未分组")) {
                has_group = true;
            }
        });

        modify_type = has_group ? "修改分组" : "选择分组";

        html.push('<strong class="modify e_group">' + modify_type);
        html.push('<div class="modify_list e_group_list">');

        $.each(group_list, function(_i, _d) {
            var s_class = (_d.groupName.contains("未分组") || _d.groupName.contains("全部") ? "hide " : "");
            s_class += ($.inArray(_d.groupID, group_ids) != -1 ? "on" : "");
            html.push('<a ' + (s_class == '' ? '' : 'class="' + s_class + '"') + ' gid="' + _d.groupID + '" title="' + _d.groupName + '">' + _d.groupName + '</a>');
        });

        html.push('</div>');
        html.push('</strong>');

        return $(html.join(''));
    };

    //为好友分组添加事件
    this.addActionFriendGroup = function(group_jQ, _ginfo, _callback) {
        var group_list_jQ = group_jQ.find('.e_group_list');
        var group_jQs = group_list_jQ.find('a');
        var uid = _ginfo.uid;

        //修改分组、选择分组
        group_jQs.each(function(_i, _group_elm) {

            var group_jQ = $(_group_elm);

            group_jQ.click(function() {

                var pre_group = $(group_jQs.filter('[class*=on]'));
                var tgt_group = group_jQ;

                var data = { "targetIds": uid, "groupId": pre_group.attr('gid'), "targetGroupId": tgt_group.attr('gid') };

                Backend.moveFriendGroup(data, function(_resp) {
                    if (Backend.resultValidate(_resp)) {
                        if (_callback) {
                            var update_count = [{ "id": pre_group.attr('gid'), "delta": -1 }, { "id": tgt_group.attr('gid'), "delta": +1}];
                            _callback(update_count);
                        }
                    } else {
                        Tip.show("修改分组失败");
                    }
                }, function() {
                    Tip.show("修改分组失败");
                });

                pre_group.removeClass('on');
                tgt_group.addClass('on');
                group_list_jQ.hide();
            })
        });

        group_jQ.hover(function() {
            group_jQ.addClass('active');
            group_list_jQ.show();
        }, function() {
            group_jQ.removeClass('active');
            group_list_jQ.hide();
        });
    };


    //创建删除好友对话框
    this.displayDelDlg = function(_uinfo, _callback) {
        Dialog.show('delfriend_dialog', {
            'child_html': MPT.getTmpl('delfriend_dialog', { "nickname": _uinfo.nickName }),
            'width': 410,
            'height': 190,
            'cache': false,
            'success': function(_dlg) {
                var submit_jQ = $('.e_submit', _dlg);
                var cancel_jQ = $('.e_cancel', _dlg);

                submit_jQ.click(function() {
                    Backend.deleteFriend({ "uid": _uinfo.uid }, function(_resp) {
                        if (Backend.resultValidate(_resp)) {
                            if (_callback) _callback();
                        } else {
                            Tip.show("删除好友失败");
                        }
                    }, function() {
                        Tip.show("删除好友失败");
                    });

                    Dialog.hide();
                });

                cancel_jQ.click(function() {
                    Dialog.hide();
                });

                Util.KeyBoardMonitor($(_dlg), 13, function() { submit_jQ.click(); });
            }
        });
    };

    //创建添加好友对话框
    this.displayAddDlg = function(_uinfo, _callback) {
        Dialog.show('invite_dialog', {
            'child_html': MPT.getTmpl('invite_dialog', _uinfo),
            'width': 380,
            'height': 250,
            'cache': false,
            'success': function(_dlg) {
                var input_jQ = $('.e_input', _dlg);
                var submit_jQ = $('.e_submit', _dlg);
                var cancel_jQ = $('.e_cancel', _dlg);
                var group_list_jQ = $('.e_group_list', _dlg);

                Backend.findFriendGroupsWithCount(null, function(_resp) {
                    if (Backend.dataValidate(_resp)) {
                        var group_list = SocialUtil.sortFriendGroup(_resp.data);
                        var html = [];
                        $.each(group_list, function(_i, _d) {
                            if (_d.groupName != '全部好友') {
                                html.push('<option value="' + _d.groupID + '">' + _d.groupName + '</option>');
                            }
                        });
                        group_list_jQ.append(html.join(''));
                        group_list_jQ.change(function() { group_list_jQ.find('option[value=-1]').remove(); });
                    }
                });

                cancel_jQ.click(function() {
                    Dialog.hide();
                })

                submit_jQ.click(function() {
                    var data = { "targetId": _uinfo.uid, "targetNickName": _uinfo.nickname, "message": input_jQ.val(), "groupID": group_list_jQ.val() };
                    Backend.sendAddFriendMessage(data, function(_resp) {
                        if (Backend.resultValidate(_resp)) {
                            if (_callback) _callback();
                            Tip.show("好友请求已发送");
                        } else if (_resp.result == 2) {
                            Tip.show("你的好友总数已满，不能再加对方为好友");
                        } else if (_resp.result == 3) {
                            Tip.show("对方已经是你的好友");
                        } else if (_resp.result == 4) {
                            Tip.show("对方好友达到上限");
                        }

                        Dialog.hide();

                    }, function() {
                        //todo:发送请求失败
                        Dialog.hide();
                    });
                });
            }
        });
    };
});
